package com.raincici.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.raincici.springcloud.service.OrderHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author R4in
 * @date 2021/8/28 11:10
 * @description
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallbackMethod")
public class OrderHystrixController {

    @Resource
    private OrderHystrixService orderHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentOK(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentOK(id);
        log.info(s);
        return s;
    }
    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    @HystrixCommand
    public String paymentTimeOut(@PathVariable("id") Integer id){
        String s = orderHystrixService.paymentTimeOut(id);
        log.info(s);
        return s;
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id){
        return "调用8001服务超时";
    }

    public String paymentGlobalFallbackMethod(){
        return "全局超时异常";
    }
}
